package cn.maihe.elg.operation.centers.fuzhou.auth.utils;

import cn.hutool.core.date.DateUtil;
import cn.maihe.elg.operation.centers.fuzhou.auth.FZAuthorization;
import cn.maihe.elg.operation.centers.fuzhou.auth.FZCenterAuthConfig;
import cn.maihe.elg.operation.centers.fuzhou.auth.FZEncryption;
import cn.maihe.elg.operation.centers.fuzhou.config.FZMakeInfoConfig;
import cn.maihe.elg.operation.centers.fuzhou.dto.FZBaseRespDTO;
import cn.maihe.elg.operation.centers.fuzhou.enums.HyMakeRespCodeEnum;
import cn.maihe.elg.operation.centers.fuzhou.exception.HyMakeException;
import cn.maihe.elg.operation.exception.CenterException;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.ClaimsStatus;
import cn.maihe.elg.operation.model.enums.RefundOrderStatus;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.utils.IdGenerate;
import cn.maihe.elg.operation.utils.NR;
import cn.maihe.elg.operation.utils.OkHttpUtils;
import cn.maihe.elg.operation.utils.RsaUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class FZApiCommonUtil {
    private static FZMakeInfoConfig fzMakeInfoConfig;


    @Autowired
    public void setFzMakeInfoConfig(FZMakeInfoConfig fzMakeInfoConfig) {
        FZApiCommonUtil.fzMakeInfoConfig = fzMakeInfoConfig;
    }

    /**
     * 1.1 获取access_token
     *
     * @return
     */
    public static String getAccessToken() {

        FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
        FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

        Map hashMap = new HashMap();
        hashMap.put("method", "get_access_token");
        hashMap.put("api_key", centerInfo.getApiKey());
        hashMap.put("api_secret", centerInfo.getApiSecret());
        hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        hashMap.put("sign", "");

        // 对数据签名
        FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
        String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

        hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
        String result = JSONObject.toJSONString(hashMap);
        log.info("签名数据: {}", result);

        FZEncryption fzEncryption = authConfig.getFZEncryption();

        try {
            // 对数据加密
//            String p = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1NkhOUxWBVm6q3/6HC5chQR9rhxdrL84EOXRV6XM+FzlwhxXcn69lTbIuf0LUmBUPlcukYwilxqLOdEJYNSQ60LghhxR28MipedO4QA2TB5WrxOX+DGCcYQ3Nnc00ci6QW7stpHol7Z8ssamffmEp5CKEBBLa9vwLtuG1RNziYdGaVVGtdsumHGuAz6XlSEcyj+L+FfkSG/jpsjnVkqm3/TBWITrbiNj/ijSYCD0wp1mLdSnzXdaJ8INFPlphqrj5apSUTzUr4bF/srefK0IJuDV4sRAnFQ/RK9oAqoJpQT6J87T1SwUFv0d96ycD+by8wat4f533Jbx8r4i0uMvkwIDAQAB";
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));
            Map param = new HashMap();
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            System.out.println(JSONObject.toJSONString(param));
//            String testAccessToken = "测试AccessToken";


            log.info("获取[福州]accessToken返回状态:{} 结果:{}", nr.getCode(), nr.getData());
            // todo 具体返回结果需要联调时候才能确定
            if (!nr.isSuccess()) {
                log.error("获取[福州]accessToken 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "获取[福州]accessToken 失败: " + nr.getErrorMessage());
            }

            JSONObject jsonObject = JSONObject.parseObject((String) nr.getData());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("获取[福州]accessToken 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "获取[福州]accessToken 失败: " + nr.getErrorMessage());
            }

            // 文档中描述，返回的是array结构的数据，其中有两个参数：access_token(访问具体业务的access_token) expired_at(access_token过期时间)
            JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
            String expiredAt = data.get("expired_at").toString();

            return data.get("access_token").toString();
        } catch (Exception e) {
            throw new RuntimeException("获取Access Token异常");
        }
    }


    /**
     * 2.2获取保函申请信息
     *
     * @param orderNo
     * @return
     */
    public static String getBhApplyInfo(String orderNo) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();


            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "insurer_get_guarantee_info");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            hashMap.put("order_no", orderNo);

            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));

            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.2获取保函申请信息]返回状态:{} 结果:{}", nr.getCode(), nr.getData());

//            NR nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/0");
            if (nr.isFail()) {
                log.error("接口请求异常[2.2获取保函申请信息]");
                throw new RuntimeException("接口请求异常[2.2获取保函申请信息]");
            }

            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.2获取保函申请信息] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.2获取保函申请信息] 失败: " + nr.getErrorMessage());
            }

            return jsonObject.get("data").toString();
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.2获取保函申请信息]");
        }
    }


    /**
     * 2.3获取企业基本信息
     *
     * @param orderNo
     * @return
     */
    public static String getCompanyInfo(String orderNo) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "insurer_get_order_company_info");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            hashMap.put("order_no", orderNo);

            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));

            // 对数据加密
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.3 获取企业基本信息]返回状态:{} 结果:{}", nr.getCode(), nr.getData());
//            NR nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/1");

            if (nr.isFail()) {
                log.error("接口请求异常[2.3 获取企业基本信息]");
                throw new RuntimeException("接口请求异常[2.3 获取企业基本信息]");
            }

            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.3获取企业基本信息] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.3获取企业基本信息] 失败: " + nr.getErrorMessage());
            }

            return jsonObject.get("data").toString();
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.3 获取企业基本信息]");
        }
    }

    /**
     * 2.4 获取文件URL
     * bondDownload 有该方法的实现，后期没有可删除
     *
     * @param fileId
     * @return
     */
    public static String getFileInfo(String fileId) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "insurer_get_file_url");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            hashMap.put("file_id", fileId);

            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            // 对数据加密
            String encryptStr = RsaUtil.encryptSegment(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);

//            NR nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/0");

            if (nr.isFail()) {
                log.error("接口请求异常[2.4 获取文件URL]");
                throw new RuntimeException("接口请求异常[2.4 获取文件URL]");
            }
            return nr.getData().toString();
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.4 获取文件URL]");
        }
    }

    /**
     * 2.5 电子保函结果推送
     *
     * @param orderNo
     * @return
     */
    public static String pushGuaranteeInfo(String orderNo) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "insurer_get_order_company_info");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            hashMap.put("order_no", orderNo);

            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));

            // 对数据加密
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.5 电子保函结果推送]返回状态:{} 结果:{}", nr.getCode(), nr.getData());
//            NR nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/1");

            if (nr.isFail()) {
                log.error("接口请求异常[2.5 电子保函结果推送]");
                throw new RuntimeException("接口请求异常[2.5 电子保函结果推送]");
            }

            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.5 电子保函结果推送] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.3获取企业基本信息] 失败: " + nr.getErrorMessage());
            }

            return jsonObject.get("data").toString();
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.5 电子保函结果推送]");
        }
    }

    /**
     * 2.8 保函状态变动
     *
     * @param orderNo
     * @return
     */
    public static String pushUpdateGuaranteeInfo(String orderNo) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "insurer_get_order_company_info");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            hashMap.put("order_no", orderNo);

            // 假设从其他地方获取到这些参数值
            String guaranteeNoOld = "DXDB1892836430846214146";
            String guaranteeNo = "DXDB1892836430846214146";
            String principal = "张三";
            String creditor = "李四";
            String guarantor = "中国银行";
            String outGuaranteeTime = "2023-01-10 10:00:00";
            String date1 = "2023-01-01";
            String date2 = "2023-01-10";
            String guaranteePdfHash = "66cf2a99d894d908f6bcf01c4a3d9d248d05ad31fa0b0dce5b0914a09b4555a4";
            String transNo = "T202301010001";
            String guaranteePdf = "file content"; // 这里是文件流，简化为字符串
            // 放入参数
            hashMap.put("order_no","C05911892836430846214145");
            hashMap.put("guarantee_no_old", guaranteeNoOld);
            hashMap.put("guarantee_no", guaranteeNo);
            hashMap.put("principal", principal);
            hashMap.put("creditor", creditor);
            hashMap.put("guarantor", guarantor);
            hashMap.put("out_guarantee_time", outGuaranteeTime);
            hashMap.put("date1", date1);
            hashMap.put("date2", date2);
            hashMap.put("guarantee_pdf_hash", guaranteePdfHash);
            hashMap.put("trans_no", transNo);
            hashMap.put("guarantee_pdf", guaranteePdf);


            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));

            // 对数据加密
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.8 保函状态变动]返回状态:{} 结果:{}", nr.getCode(), nr.getData());
//            NR nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/1");

            if (nr.isFail()) {
                log.error("接口请求异常[2.8 保函状态变动]");
                throw new RuntimeException("接口请求异常[2.8 保函状态变动]");
            }

            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.8 保函状态变动] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.8 保函状态变动] 失败: " + nr.getErrorMessage());
            }

            return jsonObject.get("data").toString();
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.8 保函状态变动]");
        }
    }

    /**
     * 2.9 保函作废
     *
     * @param acceptOrderInfoDO
     * @param voidReason
     * @return
     */
    public static JSONObject invalideGuarantee(AcceptOrderInfoDO acceptOrderInfoDO, String voidReason) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "void_order");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");
            // 其他参数
            hashMap.put("order_no", acceptOrderInfoDO.getCenterOrderId());
            hashMap.put("void_reason", voidReason);

            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));
            // 对数据加密
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.9 保函作废]返回状态:{} 结果:{}", nr.getCode(), nr.getData());
//            NR<String> nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/0");
            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.9 保函作废] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.9 保函作废] 失败: " + nr.getErrorMessage());
            }

            return jsonObject;
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.9 保函作废]");
        }
    }

    /**
     * 2.10 理赔结果
     *
     * @param refundOrderInfoDO
     * @param projectInfoDO
     * @return
     */
    public static JSONObject claimSettlement(RefundOrderInfoDO refundOrderInfoDO, ProjectInfoDO projectInfoDO) {
        try {
            FZCenterAuthConfig authConfig = fzMakeInfoConfig.getAuthConfig(CenterNoEnum.FUZHOU);
            FZMakeInfoConfig.CenterInfo centerInfo = authConfig.getCenterInfo();

            Map<String, String> hashMap = new HashMap();
            hashMap.put("method", "apply_claim");
            hashMap.put("sign", "");
            hashMap.put("timestamp", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            hashMap.put("btc_id", centerInfo.getBtcId());
            hashMap.put("enter_institution_type", "4");

//            RefundOrderInfoDO refundOrderInfo = null;


//            AcceptOrderInfoDO acceptOrderInfoDO = null;
//            ProjectInfoDO projectInfoDO = null;

            // 其他参数
            // 申请编号，全局唯一
            hashMap.put("order_no", refundOrderInfoDO.getCenterOrderId());
            // 理赔对应电子保函编号
            hashMap.put("guarantee_no", refundOrderInfoDO.getGuaranteeNo());
            // 索赔原因
            hashMap.put("claim_reason", refundOrderInfoDO.getRemark());
            // 受益人或被保险人名称
            hashMap.put("claimer_name", projectInfoDO.getTenderer());
            // 受益人或被保险人代码（统一社 会信用代码或组织机构代码）
            hashMap.put("claimer_code", projectInfoDO.getTendererCreditCode());
            // 经办人联系电话
            hashMap.put("agent_phone", projectInfoDO.getTendererContact());
            // 经办人邮箱
            hashMap.put("agent_email", "");
            // 索赔金额
            hashMap.put("amount", fen2yuanString(projectInfoDO.getBidBond()));
            // 受益人或被保险人 银行账户用于接收索赔款
            hashMap.put("receive_account", projectInfoDO.getTendererBankCode());
            // 受益人或被保险人 银行账户名称
            hashMap.put("receive_account_name", projectInfoDO.getTendererBankAccount());
            // 受益人或被保险人 收款银行开户行名称
            hashMap.put("receive_bank_name", projectInfoDO.getTendererBankOpenbank());
            // 受益人或被保险人 收款银行开户行行号(非必要字段)
            hashMap.put("receive_bank_no", "");
            // 索赔证明材料下载路径(非必要字段)
            hashMap.put("claim_evidence_url", "");


            // 对数据签名
            FZAuthorization fzAuthorization = authConfig.getFZAuthorization();
            String signature = fzAuthorization.signature(JSONObject.toJSONString(hashMap));

            hashMap.put("sign", signature);
//        System.out.println("签名数据: " + JSONObject.toJSONString(hashMap));
            String result = JSONObject.toJSONString(hashMap);
            log.info("签名数据: {}", result);

            FZEncryption fzEncryption = authConfig.getFZEncryption();
            // 对数据加密
//            String encryptStr = RsaUtil(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            String encryptStr = fzEncryption.encrypt(JSONObject.toJSONString(hashMap));
            // 对数据加密
//            String encryptStr = RsaUtil.encryptSegmenty(JSONObject.toJSONString(hashMap), centerInfo.getApiRsaPk());
            Map param = new HashMap();
            param.put("access_token", getAccessToken());
            param.put("param", encryptStr);

            NR nr = OkHttpUtils.postDataByForm(centerInfo.getApiUrl(), param);
            log.info("获取[2.10 理赔结果]返回状态:{} 结果:{}", nr.getCode(), nr.getData());

//            NR<String> nr = OkHttpUtils.httpGet("http://127.0.0.1:3000/0");
            JSONObject jsonObject = JSONObject.parseObject(nr.getData().toString());

            if (!"200".equals(jsonObject.getString("status"))) {
                log.error("[2.10 理赔结果] 失败： >> [{}]", nr.getErrorMessage());
                throw HyMakeException.of(HyMakeRespCodeEnum.FAILURE, "[2.10 理赔结果] 失败: " + nr.getErrorMessage());
            }

            return jsonObject;
        } catch (Exception e) {
            throw new RuntimeException("接口请求异常[2.10 理赔结果]");
        }
    }

    public static String fen2yuanString(Long fen) {
        try {
            return BigDecimal.valueOf(fen).divide(BigDecimal.valueOf(100L), 2, RoundingMode.HALF_UP).toString();
        } catch (Exception e) {
            throw CenterException.error("分转元失败", e);
        }
    }

    public static String calculateHash(String filePath, String algorithm) {
        StringBuilder sb = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(filePath)) {
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            byte[] byteArray = new byte[1024];
            int bytesCount;
            while ((bytesCount = fis.read(byteArray)) != -1) {
                digest.update(byteArray, 0, bytesCount);
            }
            for (byte b : digest.digest()) {
                sb.append(String.format("%02x", b));
            }
        } catch (IOException | NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String filePath = "D:\\temporary\\test.png";
        Path path = Paths.get(filePath);
        System.out.println(FZApiCommonUtil.calculateHash(filePath,"SHA-256"));
        System.out.println(FZApiCommonUtil.calculateHash(path.toString(),"SHA-256"));
    }
}
